package com.passengerflowmanagementsystem.web.controller.pf;


import com.alibaba.fastjson2.JSONObject;
import com.passengerflowmanagementsystem.common.core.controller.BaseController;
import com.passengerflowmanagementsystem.common.core.domain.AjaxResult;
import com.passengerflowmanagementsystem.statistics.domain.hk.*;
import com.passengerflowmanagementsystem.statistics.domain.vo.*;
import com.passengerflowmanagementsystem.statistics.service.IPassengerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 海康客流Controller
 *
 * @author sheng
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/pf/passenger")
public class PfPassengerController extends BaseController {

    @Autowired
    private IPassengerService passengerService;

    /**
     * 按统计组查询人脸客流统计数据
     * @return
     */
    @PostMapping("/faceFlowStatGroups")
    public AjaxResult faceFlowStatGroups(@RequestBody FaceFlowStatGroupsVo faceFlowStatGroupsVo){
        return success(passengerService.faceFlowStatGroups(faceFlowStatGroupsVo));
    }

    /**
     * 查询时间范围内的多个统计组的客流统计数据
     * @return
     */
    @PostMapping("/passengerFlowGroups")
    public AjaxResult passengerFlowGroups(@RequestBody PassengerFlowGroupsVo passengerFlowGroupsVo){
        return success(passengerService.passengerFlowGroups(passengerFlowGroupsVo));
    }

    /**
     * 查询统计组数据
     * @return
     */
    @PostMapping("/countGroupGroupsPage")
    public AjaxResult countGroupGroupsPage(@RequestBody CountGroupGroupsPageVo countGroupGroupsPageVo){
        return success(passengerService.countGroupGroupsPage(countGroupGroupsPageVo));
    }

    /**
     * 查询统计组密度统计数据
     * @return
     */
    @PostMapping("/passengerDensityGroups")
    public AjaxResult passengerDensityGroups(@RequestBody PassengerDensityGroupsVo passengerDensityGroupsVo){
        return success(passengerService.passengerDensityGroups(passengerDensityGroupsVo));
    }

    /**
     * 查询客流排行
     * @return
     */
    @PostMapping("/passengerRankingTop")
    public AjaxResult passengerRankingTop(@RequestBody PassengerRankingTopVo passengerRankingTopVo){
        return success(passengerService.passengerRankingTop(passengerRankingTopVo));
    }

    /**
     * 客流日环比
     */
    @GetMapping(value = "/PassengerFlowDailyBasis")
    public AjaxResult PassengerFlowDailyBasis(String regionId){
        //定义日环比
        DailyBasis dailyBasis = new DailyBasis();
        //根据区域获取客流统计组
        CountGroupGroupsPageVo countGroupGroupsPageVo = new CountGroupGroupsPageVo();
        countGroupGroupsPageVo.setRegionId(regionId);
        countGroupGroupsPageVo.setIsCascade(1);
        countGroupGroupsPageVo.setGroupType(1);
        countGroupGroupsPageVo.setStatType(0);
        countGroupGroupsPageVo.setPageNo(1);
        countGroupGroupsPageVo.setPageSize(20);
        String GroupStr = passengerService.countGroupGroupsPage(countGroupGroupsPageVo);
        HkRegionGroupsResult hkRegionGroupsResult = JSONObject.parseObject(GroupStr,HkRegionGroupsResult.class);
        List<String> groupsIdList = new ArrayList<>();
        if (hkRegionGroupsResult.getData().getList().length > 0){
            RegionGroups[] regionGroups = hkRegionGroupsResult.getData().getList();
            for (int i = 0; i < regionGroups.length; i++) {
                RegionGroups groups = regionGroups[i];
                groupsIdList.add(groups.getGroupId());
            }

        String groupsIds = String.join(", ", groupsIdList);
        // 获取当天客流量
        LocalDate today = LocalDate.now();// 获取当天日期
        ZoneOffset offset = ZoneOffset.ofHours(8);// 设置UTC+8时区
        // 获取当天开始时间（00:00）
        LocalDateTime startOfDay = LocalDateTime.of(today, LocalTime.MIN);
        ZonedDateTime zonedStartOfDay = startOfDay.atOffset(offset).toZonedDateTime();
        String startOfDayISO8601 = zonedStartOfDay.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        // 获取下一天开始时间（00:00）
        LocalDateTime endOfDay = LocalDateTime.of(today.plusDays(1), LocalTime.MIN);
        ZonedDateTime zonedEndOfDay = endOfDay.atOffset(offset).toZonedDateTime();
        String endOfDayISO8601 = zonedEndOfDay.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        //调取接口获取当天客流量
        dailyBasis.setTodayNum(getPassengerFlowGroups(groupsIds,startOfDayISO8601,endOfDayISO8601));
        //获取上周今日客流量
        //获取上周今日日期,和下一天日期
        LocalDate lastWeekToday = today.minusDays(7);
        LocalDateTime lastWeekTodayOfDay = LocalDateTime.of(lastWeekToday, LocalTime.MIN);
        ZonedDateTime lastWeekTodayZonedStartOfDay = lastWeekTodayOfDay.atOffset(offset).toZonedDateTime();
        String lastWeekTodayISO8601 = lastWeekTodayZonedStartOfDay.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        LocalDateTime lastWeekEndOfDay = LocalDateTime.of(lastWeekToday.plusDays(1), LocalTime.MIN);
        ZonedDateTime lastWeekEndOfDayZonedEndOfDay = lastWeekEndOfDay.atOffset(offset).toZonedDateTime();
        String lastWeekEndOfDayISO8601 = lastWeekEndOfDayZonedEndOfDay.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        //调取接口获取上周今日客流量
        dailyBasis.setLastWeekToday(getPassengerFlowGroups(groupsIds,lastWeekTodayISO8601,lastWeekEndOfDayISO8601));
        //获取上月今日客流量
        //获取上月今日日期,和下一天日期
        LocalDate lastMonthToday = today.minusMonths(1);
        LocalDateTime lastMonthTodayOfDay = LocalDateTime.of(lastMonthToday, LocalTime.MIN);
        ZonedDateTime lastMonthTodayZonedStartOfDay = lastMonthTodayOfDay.atOffset(offset).toZonedDateTime();
        String lastMonthTodayISO8601 = lastMonthTodayZonedStartOfDay.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        LocalDateTime lastMonthEndOfDay = LocalDateTime.of(lastMonthToday.plusDays(1), LocalTime.MIN);
        ZonedDateTime lastMonthEndOfDayZonedEndOfDay = lastMonthEndOfDay.atOffset(offset).toZonedDateTime();
        String lastMonthEndOfDayISO8601 = lastMonthEndOfDayZonedEndOfDay.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        //调取接口获取月今日客流量
        dailyBasis.setLastMonthToday(getPassengerFlowGroups(groupsIds,lastMonthTodayISO8601,lastMonthEndOfDayISO8601));
        //获取上年今日客流量
        //获取上年今日日期,和下一天日期
        LocalDate lastYearToday = today.minusYears(1);
        LocalDateTime lastYearTodayOfDay = LocalDateTime.of(lastYearToday, LocalTime.MIN);
        ZonedDateTime lastYearTodayZonedStartOfDay = lastYearTodayOfDay.atOffset(offset).toZonedDateTime();
        String lastYearTodayISO8601 = lastYearTodayZonedStartOfDay.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        LocalDateTime lastYearEndOfDay = LocalDateTime.of(lastYearToday.plusDays(1), LocalTime.MIN);
        ZonedDateTime lastYearEndOfDayZonedEndOfDay = lastYearEndOfDay.atOffset(offset).toZonedDateTime();
        String lastYearEndOfDayISO8601 = lastYearEndOfDayZonedEndOfDay.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        //调取接口获取上年今日客流量
        dailyBasis.setLastYearToday(getPassengerFlowGroups(groupsIds,lastYearTodayISO8601,lastYearEndOfDayISO8601));

        // 获取昨日开始时间（00:00）
        LocalDateTime yesterdayOfDay = LocalDateTime.of(today.minusDays(1), LocalTime.MIN);
        ZonedDateTime yesterdayZonedEndOfDay = yesterdayOfDay.atOffset(offset).toZonedDateTime();
        String yesterdayOfDayISO8601 = yesterdayZonedEndOfDay.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        //获取昨日客流量
        Number yesterdayNum = getPassengerFlowGroups(groupsIds,yesterdayOfDayISO8601,startOfDayISO8601);
        //计算今日和昨日客流量日环比
        Number momGrowth = ((dailyBasis.getTodayNum().doubleValue() - yesterdayNum.doubleValue()) / yesterdayNum.doubleValue()) * 100;
        dailyBasis.setDailyBasis(momGrowth);

        }

        return success(dailyBasis);
    }

    /**
     * 调取查询时间范围内的多个统计组的客流统计数据接口获取客流量
     * @param groupsIds
     * @param startTime
     * @param endTime
     * @return
     */
    public Number getPassengerFlowGroups(String groupsIds,String startTime,String endTime){
        PassengerFlowGroupsVo passengerFlowGroupsVo = new PassengerFlowGroupsVo();
        passengerFlowGroupsVo.setIds(groupsIds);
        passengerFlowGroupsVo.setGranularity("daily");
        passengerFlowGroupsVo.setStartTime(startTime);
        passengerFlowGroupsVo.setEndTime(endTime);
        String passengerFlowGroupsStr = passengerService.passengerFlowGroups(passengerFlowGroupsVo);
        HkPassengerFlowGroupsResult hkPassengerFlowGroupsResult = JSONObject.parseObject(passengerFlowGroupsStr,HkPassengerFlowGroupsResult.class);
        if (hkPassengerFlowGroupsResult.getData().getList().length>0){
            return hkPassengerFlowGroupsResult.getData().getList()[0].getNoRepeatInNum();
        }
        else {
            return 0;
        }
    }


}
